package com.ym.manager.admin.order;

import com.ym.manager.admin.order.model.EnumType;
import com.ym.manager.admin.order.model.Order;
import com.ym.manager.sys.model.AdminUser;
import com.ym.utils.DateHandler;
import com.ym.utils.Pages;
import com.ym.utils.ResultUtil;
import com.ym.utils.StringHandler;
import com.ym.web.bind.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.activation.DataHandler;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 页面：订单列表
     * @return
     */
    @RequestMapping("list")
    public String index(Model model){
        model.addAttribute("statusList", EnumType.ORDER_STATUS.values());
        return "order/list";
    }

    /**
     * 功能：订单列表
     * @param page
     * @param limit
     * @param time
     * @param user
     * @return
     */
    @RequestMapping("getList")
    @ResponseBody
    public String getList(@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "10")int limit,
                          @RequestParam(defaultValue = "")String order_sn,
                          @RequestParam(defaultValue = "0")int uid,
                          @RequestParam(defaultValue = "")String time,
                          @RequestParam(defaultValue = "0") int status,
                          @CurrentUser AdminUser user){
        Map<String,Object> result = ResultUtil.result();
        try {
            Pages<Order> pages = orderService.getList(page,limit,order_sn,uid,time,status,user.getMerchant_id());
            //查询数据
            result.put("data",pages.getList());
            result.put("count",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }


    /**
     * 页面：订单详情
     * @return
     */
    @RequestMapping("detail")
    public String detail(String order_sn,Model model){
        model.addAttribute("order",orderService.detail(order_sn));
        model.addAttribute("nowTime",new Date().getTime());
        return "/order/detail";
    }

    /**
     * 功能：变更订单状态
     * @param order_sn 订单编号
     * @param status 状态
     * @return
     */
    @RequestMapping("editOrderStatus")
    @ResponseBody
    public String editOrderStatus(@RequestParam(defaultValue = "") String order_sn,
                                  @RequestParam(defaultValue = "0") int status,
                                  @RequestParam(defaultValue = "") String trackingNumber,
                                  @RequestParam(defaultValue = "") String logistics_company,
                                  @RequestParam(defaultValue = "") String address){
        Map<String,Object> map = ResultUtil.result();
        if(StringHandler.isEmpty(trackingNumber)){
            map.put("code",400);
            map.put("msg","请填写快递单号");
            return ResultUtil.toJSON(map);
        }
        if(StringHandler.isEmpty(logistics_company)){
            map.put("code",400);
            map.put("msg","请填写快递公司");
            return ResultUtil.toJSON(map);
        }
        try {
            int i = orderService.updateOrder(order_sn,status,trackingNumber,logistics_company, DateHandler.getDate(new Date(),"yyyy-MM-dd HH:mm:ss"),address);
            if(i>0){
                map.put("msg","操作成功");
            }else if (i==-1){
                map.put("code",i);
                map.put("msg","操作错误，请联系管理人员");
            }else{
                map.put("code",i);
                map.put("msg","操作失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","系统异常，请联系管理员");
        }
        return ResultUtil.toJSON(map);
    }

    /**
     * 页面：退货列表
     * @return
     */
    @RequestMapping("back")
    public String back(){
        return "order/back";
    }

    /**
     * 功能：退货列表
     * @return
     */
    @RequestMapping("backData")
    @ResponseBody
    public String backData(@RequestParam(defaultValue = "1") int page,
                           @RequestParam(defaultValue = "10")int limit,
                           @RequestParam(defaultValue = "")String order_sn,
                           @RequestParam(defaultValue = "0")int uid,
                           @RequestParam(defaultValue = "")String time,
                           @CurrentUser AdminUser user){
        Map<String,Object> result = ResultUtil.result();
        try {
            Pages<Order> pages = orderService.back(page,limit,order_sn,uid,time);
            //查询数据
            result.put("data",pages.getList());
            result.put("count",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 功能：同意退货
     * @param order_sn 订单编号
     * @return
     */
    @RequestMapping("isBack")
    @ResponseBody
    public String isBack(@RequestParam(defaultValue = "") String order_sn,String refundAddress){
        Map<String,Object> map = ResultUtil.result();
        try {
            int i = orderService.isBack(order_sn,EnumType.ORDER_STATUS.退款申请通过.getStatus(),refundAddress);
            if(i>0){
                map.put("msg","同意退货成功");
            }else if (i==-1){
                map.put("code",i);
                map.put("msg","同意退货错误，请联系管理人员");
            }else{
                map.put("code",i);
                map.put("msg","同意退货失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","系统异常，请联系管理员");
        }
        return ResultUtil.toJSON(map);
    }

    /**
     * 功能：确认退款
     * @param id 订单id
     * @param type 类型 0 正常退款 1 未发货直接退款
     * @return
     */
    @RequestMapping("backMoney")
    @ResponseBody
    public String backMoney(String id,String order_sn,@RequestParam(defaultValue = "0") int type){
        Map<String,Object> map = ResultUtil.result();
        map.put("msg","确认退款成功");
        try{
            orderService.backMoney(id, order_sn, EnumType.ORDER_STATUS.已退款.getStatus(),type);
        }catch (Exception e){
            map.put("code",-1);
            map.put("msg",e.getMessage());
        }

        return ResultUtil.toJSON(map);
    }


}
